<template>
  <view class="goods_list">
    <my-goodslist 
    v-for="(item,inx) in goodsList" 
    :goods="item" 
    :key="inx"
    @click.native="toDetail(item)"
    ></my-goodslist>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          // 查询关键词
          query: '',
          // 商品分类id
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页显示多少条数据
          pagesize: 10
        },
        goodsList: [],
        total: '',
        // 节流阀
        isLoading: false
      };
    },
    onLoad(option){
      this.queryObj.query = option.query || ''
      this.queryObj.cid = option.cid || ''
      // 获取商品列表数据
      this.getGoodsList()
    },
    methods:{
      // 获取商品列表数据
      async getGoodsList(cb){
        this.isLoading = true// 打开节流阀
        const {data: res} = await uni.$http.get('/api/public/v1/goods/search',this.queryObj)
        this.isLoading = false // 关闭节流阀
        cb && cb()
        if(res.meta.status !== 200) return uni.$showMsg()
        this.goodsList =  [...this.goodsList,...res.message.goods] 
        this.total = res.message.total
      },
      // 点击跳转到详情页
      toDetail(item){
        uni.navigateTo({
          url:`/subpkg/goods_detail/goods_detail?id=${item.goods_id}`
        })
      }
    },
    // 上拉触底事件
    onReachBottom() {
      if(this.isLoading) return
      // 数据加载完毕
      if(this.queryObj.pagesize * this.queryObj.pagenum >= this.total) return uni.$showMsg('我是有底线的!')
      this.queryObj.pagenum = this.queryObj.pagenum + 1
      this.getGoodsList()
    },
    // 下拉刷新
    onPullDownRefresh(){
      this.queryObj.pagenum = 1,
      this.goodsList = [],
      // 传入回调关闭下拉刷新效果
      this.getGoodsList(() => {
        uni.stopPullDownRefresh()
      })
    }
  }
</script>

<style lang="scss">
  .goods_list {
    padding: 0 5px;
  }
</style>
